package cn.kickegg.log.demo;

import cn.kickegg.log.core.service.LogCollectorService;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@SpringBootApplication
@RestController
public class LogCollectorDemoApplication {
    
    private final LogCollectorService logCollectorService;
    
    public LogCollectorDemoApplication(LogCollectorService logCollectorService) {
        this.logCollectorService = logCollectorService;
    }
    
    public static void main(String[] args) {
        SpringApplication.run(LogCollectorDemoApplication.class, args);
    }
    
    @GetMapping("/test")
    public String testLog() {
        try {
            throw new RuntimeException("测试异常");
        } catch (Exception e) {
            logCollectorService.collectLog(
                "demo-app",
                "ERROR",
                e.getMessage(),
                getStackTrace(e),
                this.getClass().getName(),
                "testLog",
                Thread.currentThread().getName()
            );
        }
        return "日志已收集";
    }
    
    private String getStackTrace(Exception e) {
        StringBuilder sb = new StringBuilder();
        for (StackTraceElement element : e.getStackTrace()) {
            sb.append(element.toString()).append("\n");
        }
        return sb.toString();
    }
} 